package com.gzyj.server.factory.jtt808.down;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gzyj.data.common.VehicleIncrement;
import com.gzyj.jtt808.utils.HexBytesUtil;
import com.gzyj.jtt808.v2013.Package;
import com.gzyj.jtt808.v2019.HeaderEncryptMethod;
import com.gzyj.jtt808.v2019.HeaderMsgBodyProperty;
import com.gzyj.jtt808.v2019.protocol.jtt1078.JTT808_0x9101;
import com.gzyj.server.config.LogConfig;
import com.gzyj.server.config.SessionCache;
import com.gzyj.server.factory.CommonHandler;
import com.gzyj.server.factory.jtt808.NetWorkSerrvice;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
public class JTT808_0x9101_Handler implements CommonHandler {

    @Override
    public void handler(ChannelHandlerContext ctx, Package msg) throws Exception {

    }

    @Override
    public void handler(ChannelHandlerContext ctx, com.gzyj.jtt808.v2019.Package msg) throws Exception {

    }

    @Override
    public void handler(ChannelHandlerContext ctx, com.gzyj.jtt808.v2011.Package msg) {

    }

    @Autowired
    private NetWorkSerrvice netWorkSerrvice;

    @Override
    public void handler(VehicleIncrement vehicleInfo, String msg) {
        JSONObject command = JSON.parseObject(msg);
        String ipaddress = command.getString("ipAddress");
        String cmdId = command.getString("cmdID");
        String encIpAdress = command.getString("encIpAdress");
        Integer encPortTCP = command.getInteger("encPortTCP");
        Integer encPortUDP = command.getInteger("encPortUDP");
        Byte channel = command.getByte("channel");
        Byte dataType = command.getByte("dataType");
        Byte bitstreamType = command.getByte("bitstreamType");


        Integer seq = command.getIntValue("seq");

        if (StringUtils.isNotEmpty(vehicleInfo.getTerminalprotocoltype()) && vehicleInfo.getTerminalprotocoltype().equalsIgnoreCase("2019")) {
            JTT808_0x9101 jtt808_0x9101 = new JTT808_0x9101();
            HeaderMsgBodyProperty headerMsgBodyProperty = new HeaderMsgBodyProperty();
            headerMsgBodyProperty.setSubcontract(false);
            headerMsgBodyProperty.setEncryptMethod(HeaderEncryptMethod.None);
            headerMsgBodyProperty.setVersionFlag(true);
            jtt808_0x9101.getPackageHeader().setMsgBodyProperty(headerMsgBodyProperty);

            String phoneNumber = ipaddress;
            if (vehicleInfo.getZongduanid().length() <= 12) {
                //2019 单独处理sim卡号
                phoneNumber = HexBytesUtil.ReadBCD(HexBytesUtil.WriteBCD(phoneNumber, 20), 20);
            }

            jtt808_0x9101.getPackageHeader().setTerminalPhoneNo(phoneNumber);
            jtt808_0x9101.getPackageHeader().setProtocolVersion((byte) 1);

            jtt808_0x9101.setServerIp(encIpAdress);
            jtt808_0x9101.setTcpPort(encPortTCP);
            jtt808_0x9101.setUdpPort(encPortUDP);
            jtt808_0x9101.setChannelNo(channel);
            jtt808_0x9101.setDataType(dataType);
            jtt808_0x9101.setStreamType(bitstreamType);

            ByteBuf data = jtt808_0x9101.encode();
            netWorkSerrvice.send2Veh(phoneNumber, data);
            LogConfig.commondLog.info("808 2019版 9101 下发 实时音视频传输请求 指令 {} {}", phoneNumber, "待实现");

        } else {

            com.gzyj.jtt808.v2013.protocol.jtt1078.JTT808_0x9101 jtt808_0x9101 = new com.gzyj.jtt808.v2013.protocol.jtt1078.JTT808_0x9101();
            com.gzyj.jtt808.v2013.HeaderMsgBodyProperty headerMsgBodyProperty = new com.gzyj.jtt808.v2013.HeaderMsgBodyProperty();

            headerMsgBodyProperty.setSubcontract(false);
            headerMsgBodyProperty.setEncryptMethod(com.gzyj.jtt808.v2013.HeaderEncryptMethod.None);
            jtt808_0x9101.getPackageHeader().setMsgBodyProperty(headerMsgBodyProperty);
            jtt808_0x9101.getPackageHeader().setTerminalPhoneNo(ipaddress);

            jtt808_0x9101.setServerIp(encIpAdress);
            jtt808_0x9101.setTcpPort(encPortTCP);
            jtt808_0x9101.setUdpPort(encPortUDP);
            jtt808_0x9101.setChannelNo(channel);
            jtt808_0x9101.setDataType(dataType);
            jtt808_0x9101.setStreamType(bitstreamType);
            ByteBuf data = jtt808_0x9101.encode();
            netWorkSerrvice.send2Veh(ipaddress, data);

            if (!SessionCache.THStorage.containsKey(ipaddress)) {
                Thread th = new Thread(() -> {
                    while (true){
                        com.gzyj.jtt808.v2013.protocol.jtt1078.JTT808_0x9105 jtt808_0x9105 = new com.gzyj.jtt808.v2013.protocol.jtt1078.JTT808_0x9105();
                        com.gzyj.jtt808.v2013.HeaderMsgBodyProperty headerMsgBodyProperty1 = new com.gzyj.jtt808.v2013.HeaderMsgBodyProperty();

                        headerMsgBodyProperty1.setSubcontract(false);
                        headerMsgBodyProperty1.setEncryptMethod(com.gzyj.jtt808.v2013.HeaderEncryptMethod.None);
                        jtt808_0x9105.getPackageHeader().setMsgBodyProperty(headerMsgBodyProperty1);
                        jtt808_0x9105.getPackageHeader().setTerminalPhoneNo(ipaddress);

                        jtt808_0x9105.setChannelNo(channel);
                        jtt808_0x9105.setDropRate((byte) 0);
                        ByteBuf jtdata = jtt808_0x9105.encode();
                        netWorkSerrvice.send2Veh(ipaddress, jtdata);
                        try {
                            Thread.sleep(5000L);
                            LogConfig.commondLog.info("808 2013版 9105 下发 实时音视频传输状态通知 指令 {}", ipaddress);
                        } catch (InterruptedException e) {

                        }
                    }


                });
                th.start();
                SessionCache.THStorage.put(ipaddress, th);
            }

            LogConfig.commondLog.info("808 2013版 9101 下发 实时音视频传输请求 指令 {} {}", ipaddress, "待实现");

        }

    }
}
